package com.cmbchina.encrypt;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Base64Utils;

import java.util.Base64;

/**
 * SM4Util工具类
 *
 * @author liuxb
 * @date 2022/1/18 8:36
 */
@Slf4j
public class SM4Util {
    /**
     * 默认密码
     */
    private static final String SM4_KEY = "123456";

    public static String sm4ECBEncrypt(String key, String plntxt) {
        if (StringUtils.isBlank(plntxt)) {
            return plntxt;
        }
        try {
            byte[] ciper = SM4.getInstance().CMBSM4EncryptWithECB(key.getBytes("UTF-8"), plntxt.getBytes("UTF-8"));
            return Base64Utils.encodeToString(ciper);
        } catch (Exception e) {
            return plntxt;
        }
    }

    public static String sm4ECBEncrypt(String plntxt) {
        return sm4ECBEncrypt(SM4_KEY, plntxt);
    }

    public static String sm4ECBDecrypt(String key, String plntxt) {
        if (StringUtils.isBlank(plntxt)) {
            return plntxt;
        }
        try {
            byte[] plain = SM4.getInstance().CMBSM4DecryptWithECB(key.getBytes("UTF-8"), Base64Utils.decodeFromString(plntxt));
            return new String(plain);
        } catch (Exception e) {
            return plntxt;
        }
    }

    public static String sm4ECBDecrypt(String plntxt) {
        return sm4ECBDecrypt(SM4_KEY, plntxt);
    }

    /**
     * SM4解密返回报文
	 * @param input_base64 
	 * @param key 
	 * @return
     */
    public static String decryptBody(String input_base64, String key) {
        try {
            byte[] res = SM4.getInstance().CMBSM4DecryptWithCBC(key.getBytes(), key.getBytes(), Base64.getDecoder().decode(input_base64));
            String result = new String(res);
            log.info("******************SM4 decrypt success: " + result + "*******************");
            return result;
        } catch (SMCryptException e) {
            log.error("******************SM4 decrypt fail*******************");
            return null;
        }
    }

    /**
     * CBC模式
     * @param key
     * @param plntxt
     * @return
     */
    public static String sm4CBCEncrypt(String key, String plntxt) {
        if (StringUtils.isEmpty(plntxt)) {
            return plntxt;
        }
        try {
            byte[] ciper = SM4.getInstance().CMBSM4EncryptWithCBC(key.getBytes("UTF-8"), key.getBytes("UTF-8"), plntxt.getBytes("UTF-8"));
            return Base64.getEncoder().encodeToString(ciper);
        } catch (Exception e) {
            return plntxt;
        }
    }

    public static String sm4CBCDecrypt(String key, String plntxt) {
        if (StringUtils.isEmpty(plntxt)) {
            return plntxt;
        }
        try {
            byte[] plain = SM4.getInstance().CMBSM4DecryptWithCBC(key.getBytes("UTF-8"), key.getBytes("UTF-8"), Base64.getDecoder().decode(plntxt));
            return new String(plain);
        } catch (Exception e) {
            return plntxt;
        }
    }


    public static void main(String[] args) {
        String key = "123456";
        String plntxt = "tKnAst75nCdrfI/JgWlt0gjrYrtIZl9d57M6UEKMI0Ybf7wkOw1WuASqPlk76wlwOpkPmhpyHbdtAHXl0w/nRhottGxAUSPN56O+oe4O7mXg6ZwPauncyBpO/J3cm5LiZ/c2RFJ0aGPYnEDu/UwygZ8G9Q1gF7mOH+edyvcEORV7TH8+Eouv49KJ3Kv4cTUSUiso6HGVcW6sKarXxZldDpWHjzz7qkr9TIj/td/UJLIanY7652dZwaMB4wMWrooHF+WcSV3nFD/SjGTudGLLGJq85DU5mPvYzxYMdsqDdM37EJVO/t15Ja+5O/tl6mSyIFKvAQ+NH8/IuT6IxalZqnU5mj9Coc925FURu9+7lxt+l1hl5nhJy3NMxIRBwLjUhbV+W+3j2l+YooppgUnoiGFuHZheMo884NGGimldHqMtLzmvrMRERi3oqWOFfT100B5ogbDZYYbcBF0vUsw605pBQHGj6pP25U4gNay6A/oWcZjmQ01PcDlM2nBNr9OoBSx4ToPAB6ahYJ1svOqxgJEVS4LYcvquu43cyNDpBpktMwYFuvEYW+OI2A/s7Kp+sNnzYkvT6rZ/4NDqSknHQm506eHgoL1KmSz/oFmm7JNXbYGnyuPNQK298FmtkyOWpX3ZHkrW6j9+/i2irTZQuwTQMsF+CMqKlR4CQbMbKdTEv1UAEUa1EjafBaq7HjPj6jP7MQk4HJwMcVXr5JrbQVnBrRRC0DIlM8CptUHCG+W/iZH/MjhIZjLlWF6TGWVnA3ymV144eTh4wQIARjUIRA==";

        String s = decryptBody(plntxt, key);
        System.out.println(s);

    }


}
